package com.xmtlxx.web.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.xmtlxx.common.annotation.Log;
import com.xmtlxx.common.annotation.RepeatSubmit;
import com.xmtlxx.common.core.controller.BaseController;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.domain.R;
import com.xmtlxx.common.core.domain.entity.SysWorkflow;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.core.validate.AddGroup;
import com.xmtlxx.common.core.validate.EditGroup;
import com.xmtlxx.common.core.validate.QueryGroup;
import com.xmtlxx.common.enums.BusinessType;
import com.xmtlxx.common.utils.poi.ExcelUtil;
import com.xmtlxx.system.domain.bo.SysWorkflowBo;
import com.xmtlxx.system.domain.vo.SysNodeVo;
import com.xmtlxx.system.domain.vo.SysWorkflowVo;
import com.xmtlxx.system.service.ISysRoleService;
import com.xmtlxx.system.service.ISysWorkflowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 工作流程Controller
 *
 * @author xmtlxx
 * @date 2022-04-24
 */
@Validated
@Api(value = "工作流程控制器", tags = {"工作流程管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/sysWorkflow")
public class SysWorkflowController extends BaseController {

    private final ISysWorkflowService iSysWorkflowService;

    private final ISysRoleService roleService;

    /**
     * 返回一个初始化的对象到前端，方便进行动态渲染
     */
    @ApiOperation("用于返回一个初始化对象")
    @PostMapping("/initBean")
    R<SysWorkflowVo> initBean(){
        SysWorkflowVo vo = new SysWorkflowVo();
        //可以对数据进行初始化
        List<SysNodeVo> items = new ArrayList<>();
        SysNodeVo start = new SysNodeVo();
        start.setSflag("Y");
        start.setName("开始");
        start.setSort(0L);
        start.setStatus("0");

        SysNodeVo end = new SysNodeVo();
        end.setEflag("Y");
        end.setName("结束");
        end.setSort(1L);
        end.setStatus("0");
        items.add(start);
        items.add(end);
        vo.setItems(items);
        return R.ok(vo);
    }

    /**
     * 查询工作流程列表
     */
    @ApiOperation("查询工作流程列表")
    @SaCheckPermission("system:sysWorkflow:list")
    @GetMapping("/list")
    public TableDataInfo<SysWorkflowVo> list(@Validated(QueryGroup.class) SysWorkflowBo bo, PageQuery pageQuery) {
        return iSysWorkflowService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出工作流程列表
     */
    @ApiOperation("导出工作流程列表")
    @SaCheckPermission("system:sysWorkflow:export")
    @Log(title = "工作流程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated SysWorkflowBo bo, HttpServletResponse response) {
        List<SysWorkflowVo> list = iSysWorkflowService.queryList(bo);
        ExcelUtil.exportExcel(list, "工作流程", SysWorkflowVo.class, response);
    }

    /**
     * 获取工作流程详细信息
     */
    @ApiOperation("获取工作流程详细信息")
    @SaCheckPermission("system:sysWorkflow:query")
    @GetMapping("/{wid}")
    public R<SysWorkflowVo> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("wid") String wid) {
        return R.ok(iSysWorkflowService.queryById(wid));
    }

    /**
     * 新增工作流程
     */
    @ApiOperation("新增工作流程")
    @SaCheckPermission("system:sysWorkflow:add")
    @Log(title = "工作流程", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysWorkflowVo vo) {
//        SysWorkflowBo bo = new SysWorkflowBo();
//        BeanUtil.copyProperties(vo,bo);
        return toAjax(iSysWorkflowService.insertByBo(vo) ? 1 : 0);
    }

    /**
     * 修改工作流程
     */
    @ApiOperation("修改工作流程")
    @SaCheckPermission("system:sysWorkflow:edit")
    @Log(title = "工作流程", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysWorkflowVo vo) {
//        SysWorkflowBo bo = new SysWorkflowBo();
//        BeanUtil.copyProperties(vo,bo);
        return toAjax(iSysWorkflowService.updateByBo(vo) ? 1 : 0);
    }

    /**
     * 删除工作流程
     */
    @ApiOperation("删除工作流程")
    @SaCheckPermission("system:sysWorkflow:remove")
    @Log(title = "工作流程", businessType = BusinessType.DELETE)
    @DeleteMapping("/{wids}")
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable String[] wids) {
        return toAjax(iSysWorkflowService.deleteWithValidByIds(Arrays.asList(wids), true) ? 1 : 0);
    }

    /**
     * 删除工作流程
     */
    @ApiOperation("状态修改")
    @SaCheckPermission("system:sysWorkflow:edit")
    @Log(title = "工作流程", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public R<Void> changeStatus(@RequestBody SysWorkflow enty) {
        return toAjax(iSysWorkflowService.updateWorkflowStatus(enty));
    }
}
